package com.hotacorp.opencloud.userservice.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.userservice.entity.GatewayDefineEntity;
import com.hotacorp.opencloud.userservice.service.GatewayDefineService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 路由定义表
 *
 * @author lwg
 * @email lwg@gmail.com
 * @date 2019-12-26 11:20:25
 */
@RestController
@Api(tags = "路由管理API")
public class GatewayDefineController {
    @Autowired
    private GatewayDefineService gatewayDefineService;

    /**
     * 列表
     */
    @GetMapping(value = "/gatewaydefine/list")
    @PreAuthorize("hasAuthority('userservice:gatewaydefine:list')")
	@ApiOperation(value = "查询路由")
    @LogAnnotation(module = "路由")
    public Result<?> list(@RequestParam Map<String, Object> params){
        PageData page = gatewayDefineService.queryPage(params);

        return Result.succeed("succeed", page);
    }


    /**
     * 信息
     */
    @GetMapping(value = "/gatewaydefine/info/{id}")
    @PreAuthorize("hasAuthority('userservice:gatewaydefine:info')")
	@ApiOperation(value = "路由详情")
    @LogAnnotation(module = "路由")
    public Result<?> info(@PathVariable("id") String id){
		GatewayDefineEntity gatewayDefine = gatewayDefineService.getById(id);

        return Result.succeed("succeed", gatewayDefine);
    }

    /**
     * 保存
     */
    @PostMapping(value = "/gatewaydefine/save")
    @PreAuthorize("hasAuthority('userservice:gatewaydefine:save')")
	@ApiOperation(value = "添加路由")
    @LogAnnotation(module = "路由")
    public Result<?> save(@RequestBody @Validated(AddGroup.class) GatewayDefineEntity gatewayDefine){
		gatewayDefineService.save(gatewayDefine);

        return Result.succeed();
    }

    /**
     * 修改
     */
    @PostMapping(value = "/gatewaydefine/update")
    @PreAuthorize("hasAuthority('userservice:gatewaydefine:update')")
	@ApiOperation(value = "修改路由")
    @LogAnnotation(module = "路由")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) GatewayDefineEntity gatewayDefine){
		gatewayDefineService.updateById(gatewayDefine);

        return Result.succeed();
    }

    /**
     * 删除
     */
    @PostMapping(value = "/gatewaydefine/delete")
    @PreAuthorize("hasAuthority('userservice:gatewaydefine:delete')")
	@ApiOperation(value = "删除路由")
    @LogAnnotation(module = "路由")
    public Result<?> delete(@RequestBody String[] ids){
		gatewayDefineService.removeByIds(Arrays.asList(ids));

        return Result.succeed();
    }

    @GetMapping(value = "/anon/synroute")
    @ApiOperation(value = "同步路由")
    @ApiIgnore
    public Result<?> synRoute() {
    	int ddd = gatewayDefineService.synroute();
    	if (ddd != 1) {
    		return Result.failed("路由同步失败");
    	} else {
        	return Result.succeed("succeed", ddd);
		}
    }

}
